<%@ page import="com.qimei.onerice.entity.DishCategory" %>
<%@ page import="com.qimei.onerice.service.impl.DishCategoryServiceImpl" %>
<%@ page import="java.util.List" %>
<%@ page import="com.qimei.onerice.entity.Dish" %>
<%@ page import="com.qimei.onerice.service.impl.DishServiceImpl" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: aaa
  Date: 2023/12/11
  Time: ฅ(΅•ㅅ•΅❀)ฅ 15:06
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
	<title>商店</title>
	<style>
		.custom-btn:hover {
			background-color: transparent;
		}
	</style>

	<link rel="stylesheet" href="<c:url value="/css/bootstrap.min.css"/>">
	<script src="<c:url value="/js/bootstrap.bundle.min.js"/>"></script>
	<script src="<c:url value="/js/bootstrap.min.js"/>"></script>
</head>
<body>
<%--<%@ include file="/view/common/header.jsp" %>--%>
<jsp:include page="/view/common/header.jsp"></jsp:include>
<main>
	<div class="album py-5 bg-light">
		<div class="container">

			<div class="p-3 mb-3 border-bottom">
				<div class="container">

					<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start" style="margin-top: -20px">
						<a href="#" class="d-flex align-items-center mb-2 mb-lg-0 text-dark text-decoration-none">
							<svg class="bi me-2" width="40" height="32" role="img" aria-label="Bootstrap"><use xlink:href="#bootstrap"/></svg>
						</a>

						<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
							<li><a href="store.jsp" class="nav-link px-2 link-dark">所有菜品</a></li>
							<%
								List<DishCategory> dishCategoryList = new DishCategoryServiceImpl().getDishCategoryList();
								for (DishCategory dishCategory : dishCategoryList) {
							%>
							<li><a href="store.jsp?categoryId=<%= dishCategory.getCategoryId() %>" class="nav-link px-2 link-dark"><%= dishCategory.getCategoryName() %></a></li>
							<%
								}
							%>
						</ul>

						<div class="col-12 col-lg-auto mb-2 mb-lg-0 me-lg-3 d-flex align-items-center" style="padding-top: 17px">
							<form class="input-group" role="search" action="store.jsp" method="get">
								<input type="search" class="form-control" placeholder="搜索菜品..." aria-label="Search" name="dishName">
								<button type="submit" class="btn btn-outline-secondary custom-btn border-0">
									<img src="<c:url value='/img/common/search.png'/>" alt="Search" width="16px" height="16px">
								</button>
							</form>
						</div>

					</div>
				</div>
			</div>

			<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
				<%
					// 获取传递的categoryId
					String categoryId = request.getParameter("categoryId");
					String dishName = request.getParameter("dishName");

					List<Dish> dishList;
					if (dishName != null && !dishName.isEmpty()) {
						dishList = new DishServiceImpl().getDishListByNameLike(dishName);
					} else if (categoryId != null) {
						dishList = new DishServiceImpl().getDishListByCategoryId(Integer.parseInt(categoryId));
					} else {
						dishList = new DishServiceImpl().getDishList();
					}
					pageContext.setAttribute("dishList", dishList);
				%>
				<c:forEach var="dish" items="${dishList}">
					<div class="col">
						<div class="card shadow-sm">
							<img class="bd-placeholder-img card-img-top" width="100%" height="225" src="<c:url value='/img/dish/${dish.dishImage}'/>" alt="game">
							<div class="card-body">
								<h4 class="card-title font-weight-bold">${dish.name}</h4>

								<!-- 游戏信息 -->
								<div class="mt-3">
									<p class="card-text">价格: ${dish.price}￥</p>
									</p>
								</div>

									<div class="d-flex justify-content-between align-items-center mt-3">
										<div class="btn-group">
											<a href="<c:url value="/order?customerId=${sessionScope.user.customerId}&dishId=${dish.dishId}&action=add"/>" class="btn btn-primary w-30 mx-auto">添加到购物车</a>
											<a href="<c:url value="/view/dishInfo.jsp?dishId=${dish.dishId}"/>" class="btn btn-primary w-15 mx-auto">详情</a>
										</div>
									</div>
							</div>
						</div>
					</div>
				</c:forEach>
			</div>
		</div>
	</div>
</main>
<jsp:include page="/view/common/footer.jsp"></jsp:include>
<script>
	function redirectToDishInfo(dishId) {
		let url = '<c:url value="/view/dishInfo.jsp"/>';
		window.location.href = url + '?dishId=' + dishId;
	}
	function login(dishId) {
		let url = '<c:url value="/login.jsp"/>';
		window.location.href = url + '?dishId=' + dishId;
	}

</script>
</body>
</html>
